//
//  WAMyTeamListController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/11/3.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAMyTeamListController.h"
#import "WNTeamModel.h"
#import "WATeamCell.h"

@interface WAMyTeamListController ()<UITextFieldDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSMutableArray *dataSouces;

@property (assign, nonatomic) NSInteger type;//0:一级好友  1：二级好友  2：三级好友
@end

@implementation WAMyTeamListController

- (instancetype)initWtihType:(NSInteger)type
{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WATeamCell" bundle:nil] forCellReuseIdentifier:@"WATeamCell"];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.tableFooterView = [UIView new];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestData{
    [self  requestDataSucc:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WNTeamModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        [self.dataSouces addObjectsFromArray:memberList];
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.tableView reloadEmptyDataSet];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [WNTool show:mes];
    }];
}

- (void)requestDataSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
//    if (self.type == 0) {
//        [WNBusinessRequest getTaskListWithPageNum:self.pageNum Succ:succ fail:fail];
//    }else if(self.type == 1){
//        [WNBusinessRequest getMyTaskListWithPageNum:self.pageNum status:0 Succ:succ fail:fail];
//    }else if(self.type == 2){
//        [WNBusinessRequest getMyTaskListWithPageNum:self.pageNum status:1 Succ:succ fail:fail];
//    }
    [WNBusinessRequest getFriendListWithType:self.type pageNum:self.pageNum Succ:succ fail:fail];
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WATeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WATeamCell"];
    [cell configWithModel:self.dataSouces[indexPath.row] type:self.type];
//    [cell setButtonActionBlock:^(WATaskModel * _Nonnull model) {
//        [self exchangeWithModel:model];
//    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}
@end
